package crm.salesOrderProcessing.session;

import java.text.DecimalFormat;
import javax.ejb.EJB;
import crm.salesOrderProcessing.entity.ProductEntity;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
//import javax.persistence.EntityManager;
//import javax.persistence.PersistenceContext;
import crm.salesOrderProcessing.entity.LineItemEntity;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import crm.salesOrderProcessing.util.exception.IllegalCompanyTypeException;
import crm.salesOrderProcessing.util.exception.PriceUnacceptableException;
import crm.salesOrderProcessing.util.exception.SalesException;
import commonInfrastructure.system.entity.SystemParameterEntity;
import commonInfrastructure.system.session.SystemSessionBean;

@Stateless
@LocalBean
public class PriceManagementSessionBean {

//    @PersistenceContext
//    private EntityManager em;
    
    @EJB
    SystemSessionBean systemSessionBean;
    
    private SystemParameterEntity systemParameter;
    public double unitPrice = 0.0;


    public double[] calculatePirce(String companyType, ProductEntity product, int qty) throws SalesException {
        systemParameter = systemSessionBean.getSystemParameter();
        
        if (product.getPromotionPrice() < product.getBasePrice()) {
            unitPrice = product.getBasePrice();
        } else {
            unitPrice = product.getPromotionPrice();
        }
        
        double[] priceBlock = new double[2];  
        priceBlock[0] = 1.0;

        if (companyType.equals(systemParameter.getDS_COMPANY_TYPE())) {  //companyTypeCode DS for Direct Sales
            if (qty > systemParameter.getDsLevelOneQuantity() && qty <= systemParameter.getDsLevelTwoQuantity()) {
                unitPrice = unitPrice * systemParameter.getDsLevelOneDiscount();
                priceBlock[0] = systemParameter.getDsLevelOneDiscount();
            } else if (qty > systemParameter.getDsLevelTwoQuantity()) {
                unitPrice = unitPrice * systemParameter.getDsLevelTwoDiscount();
                priceBlock[0] = systemParameter.getDsLevelTwoDiscount();
            }
        } else if (companyType.equals(systemParameter.getWS_COMPANY_TYPE())) { //companyTypeCode WS for Whole Sales
            unitPrice = unitPrice * systemParameter.getWsInitialDiscount();
            if (qty > systemParameter.getWsLevelOneQuantity() && qty <= systemParameter.getWsLevelTwoQuantity()) {
                unitPrice = unitPrice * systemParameter.getWsLevelOneDiscount();
                priceBlock[0] = systemParameter.getWsLevelOneDiscount()*systemParameter.getWsInitialDiscount();
            } else if (qty > systemParameter.getWsLevelTwoQuantity()) {
                unitPrice = unitPrice * systemParameter.getWsLevelTwoDiscount();
                priceBlock[0] = systemParameter.getWsLevelTwoDiscount()*systemParameter.getWsInitialDiscount();
            }
        } else //invalid companyTypeCode
        {
            throw new IllegalCompanyTypeException();
        }

        priceBlock[1] = roundTwoDecimals(unitPrice);
        
        return priceBlock;

    }

    public void verifySalesOrderPrice(SalesOrderEntity salesOrder) throws PriceUnacceptableException, IllegalCompanyTypeException {
        
        int qty;
        double price;
        systemParameter = systemSessionBean.getSystemParameter();
        
        if (salesOrder.getCustomer().getCompanyType().equals(systemParameter.getDS_COMPANY_TYPE())) {

            for (LineItemEntity lineItem : salesOrder.getSalesOrderLineItems()) {

                if (lineItem.getProduct().getPromotionPrice() < lineItem.getProduct().getBasePrice()) {

                    unitPrice = lineItem.getProduct().getBasePrice();
                } else {

                    unitPrice = lineItem.getProduct().getPromotionPrice();
                }
                qty = lineItem.getQuantity();
                price = lineItem.getPrice();

                if (((qty < systemParameter.getDsLevelOneQuantity()) && (price < roundTwoDecimals(unitPrice * systemParameter.getDsInitialDiscount())))
                        || ((qty < systemParameter.getDsLevelTwoQuantity()) && (price < roundTwoDecimals(unitPrice * systemParameter.getDsLevelOneDiscount() * systemParameter.getDsInitialDiscount())))
                        || (price < roundTwoDecimals(unitPrice * systemParameter.getDsLevelTwoDiscount() * systemParameter.getDsInitialDiscount()))) {
                    throw new PriceUnacceptableException();
                }

            }
            return;
        } else if (salesOrder.getCustomer().getCompanyType().equals(systemParameter.getWS_COMPANY_TYPE())) {


            for (LineItemEntity lineItem : salesOrder.getSalesOrderLineItems()) {

                if (lineItem.getProduct().getPromotionPrice() < lineItem.getProduct().getBasePrice()) {

                    unitPrice = lineItem.getProduct().getBasePrice();
                } else {

                    unitPrice = lineItem.getProduct().getPromotionPrice();
                }
                qty = lineItem.getQuantity();
                price = lineItem.getPrice();
//                System.out.println("Qty " + qty + "   price  " + price + " Unit price  " + unitPrice * systemParameter.getWsInitialDiscount() * systemParameter.getWsLevelOneDiscount());
//                System.out.println(qty < systemParameter.getWsLevelOneQuantity());
//                System.out.println(price < unitPrice * systemParameter.getWsInitialDiscount());
//                System.out.println(qty < systemParameter.getWsLevelTwoQuantity());
//                System.out.println(price < roundTwoDecimals(unitPrice * systemParameter.getWsLevelOneDiscount() * systemParameter.getWsInitialDiscount()));
                if (((qty < systemParameter.getWsLevelOneQuantity()) && (price < roundTwoDecimals(unitPrice * systemParameter.getWsInitialDiscount())))
                        || ((qty < systemParameter.getWsLevelTwoQuantity()) && (price < roundTwoDecimals(unitPrice * systemParameter.getWsLevelOneDiscount() * systemParameter.getWsInitialDiscount())))
                        || (price < roundTwoDecimals(unitPrice * systemParameter.getWsLevelTwoDiscount() * systemParameter.getWsInitialDiscount()))) {
                    throw new PriceUnacceptableException();
                }

            }
            return;
        } else {
            throw new IllegalCompanyTypeException();
        }

    }

    public double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
}
